<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\api\controller;

use app\api\model\Api;
use app\api\model\JWT;
use think\App;
use think\Controller;
use think\Db;
use app\api\model\Role as RoleModel;

/**
 * @title 客户管理
 * @description 接口说明
 * @header name:flag require:1 default: desc:手机设备号
 */
class Client extends Api
{
    /**
     * @title 客户
     * @description 接口说明
     * @author 开发者
     * @url /api/Client/index
     * @method POST
     * @param name:page type:int require:1 default: other: desc:页码
     * @param name:type type:int require:1 default: other: desc:1宴会客户2零点客户
     * @param name:manager_id type:int require:1 default: other: desc:客户经理
     * @param name:search type:string require:0 default: other: desc:搜索内容
     * @param name:begin_time type:string require:0 default: other: desc:开始时间（...-..-..）
     * @param name:end_time type:string require:0 default: other: desc:结束时间（...-..-..）
     * 
     * @return list:列表@
     * @list id:客户id name:客户名 num:消费次数 mobile:客户手机号 money:消费金额 manager:客户经理
     * @return page:总页数
     * @return total:总条数
     */
   public function index()
   {
        $user = $this->user;
        $type = input('type');
        $page = input('page')??1;
        if(!input('type')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $begin_time = input('begin_time');
        $end_time = input('end_time');
        if($begin_time && $end_time){
            $begin = strtotime($begin_time);
            $end = strtotime($end_time);
            $where[] = ['addtime','between',[$begin,$end]];
        }
        $manager_id = input('manager_id');
        if($manager_id){
            $where[] = ['manager_id','=',$manager_id];

        }
        $search = input('search');
        if(is_numeric($search)){
            $where[] = ['mobile','like',"%".$search."%"];
        }else{
            $where[] = ['name','like',"%".$search."%"];
        }
        $where[] = ['hid','=',$user['hid']];
        if($type == 1){
            $list = db::name('client')->where($where)->field('id,name,num,mobile,money,manager')->order('num desc')->page("$page,10")->select();
            $total = db::name('client')->where($where)->count();
        }else{
            $list = db::name('client_zero')->where($where)->field('id,name,num,mobile,manager')->order('num desc')->page("$page,10")->select();           
            $total = db::name('client')->where($where)->count();
        }
        $data['list'] = $list;
        $data['page'] = ceil($total/10);
        $data['total'] = $total;
        return $this->ajaxReturn(0,'成功',$data);  
   }
    /**
     * @title 客户信息
     * @description 接口说明
     * @author 开发者
     * @url /api/Client/infor
     * @method POST
     * @param name:id type:int require:1 default: other: desc:客户id
     * @param name:type type:int require:1 default: other: desc:1宴会2零点
     * 
     * @return list:列表@
     * @list id:客户id name:客户名 avatar:头像id avatars:客户头像url other:其他图片id image:其他图片 mobile:客户手机号 unit:单位 position:职务 address:住址 birthday:生日 likes:喜好 diet:忌口 content:备注 sex:性别1男2女 
     * @return page:总页数
     */
    public function infor()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        if(!input('type')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $type = input('type');

        $client = db::name('client')->where('id',$id)->find();
        if(!$client){
            return $this->ajaxReturn(1,'订单异常');
        }
      
            if($client['manager_id'] != $user['id']){

                $pid = db::name('admin_role')->where('id','in',$user['role'])->field('pid')->select();
                $pids = 0;
                foreach ($pid as $vs) {
                    if($vs['pid'] == 0){
                        $pids = 1;
                        break;
                    }else{
                        $pids = 2;
                    }
                }
                if($pids == 2){
                    $res = RoleModel::roleAuth($user['role']);
                    $newArrs = [];
                    if($res){
                        foreach ($res as $k => $v) {
                            $str1 =str_replace("[","",$res[$k]);
                            $str1 =str_replace("]","",$str1);
                            $newArr = explode(',',$str1);
                            $newArrs = array_merge($newArr,$newArrs);
                        }
                        foreach ($newArrs as $kn => $vn) {
                            $vn =str_replace('"',"",$vn);
                            $newArrs[$kn] = $vn;
                        }
                        if($type == 1){
                            if(!in_array('327',$newArrs)){
                                return $this->ajaxReturn(1,'暂无权限');
                            }
                        }elseif($type == 2){
                            if(!in_array('328',$newArrs)){
                                return $this->ajaxReturn(1,'暂无权限');
                            }
                        }
                       
                    }
                }
        
            }
        
        


        $where[] = ['id','=',$id];
        if($type == 1){
            $list = db::name('client')->where($where)->field('id,name,avatar,mobile,unit,position,address,birthday,likes,diet,content,other,sex')->find();
        }elseif($type == 2){
            $list = db::name('client_zero')->where($where)->field('id,name,avatar,mobile,unit,position,address,birthday,likes,diet,content,other,sex')->find();
        }
        $list['avatars'] =  get_file_path($list['avatar']);
        if($list['other']){
            $other = explode( ",",$list['other']);
            if(count($other) != 1){
                $pic = array();
                foreach($other as $vs){
                    $pic[] = get_file_path($vs);
                }
                $list['image'] = $pic;
            }else{
                $pics = array();
                $pics[] = get_file_path($list['other']);
                $list['image'] = $pics;
            }
        }
        $data['list'] = $list;
        return $this->ajaxReturn(0,'成功',$data);

    }
    /**
     * @title 客户历史订单
     * @description 接口说明
     * @author 开发者
     * @url /api/Client/user_order
     * @method POST
     * @param name:id type:int require:1 default: other: desc:客户id
     * @param name:page type:int require:1 default: other: desc:页码
     * 
     * @return list:列表@
     * @list id:订单id is_meeting:1会议2宴会 banquet:宴会厅名称 banquet_type:宴会类型 desk:桌数（为会议时显示人数） prepare_desk:备桌数 scheduled_time:预定时间 start_time:宴会日期 end_time:结束日期（会议订单才有此参数）
     * @return page:总页数
     */
    public function user_order()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $page = input('page')??1;
        $mobile = db::name('client')->where('id',$id)->value('mobile');
        $wheres[] = ['director_id','=',$user['id']];
        $wheres[] = ['hid','=',$user['hid']];
        $wheres[] = ['mobile','=',$mobile];
        // 用户订单
        $order = db::name('scheduled')
        ->where($wheres)
        ->field('id,bid,tid,desk,prepare_desk,is_meeting,addtime,begin_year,begin_month,begin_day,begin_night,end_year,end_month,end_day,end_night')
        ->page("$page,10")->select();
        $total = db::name('scheduled')->where($wheres)->count();
        if($order){
            foreach ($order as $k => $v) {
                // 宴会厅
                if($v['bid']){
                    $banquet = db::name('banquet')->where('id',$v['bid'])->field('id,name')->find();
                    if($banquet){
                        $order[$k]['banquet'] = urldecode($banquet['name']);
                    }
                }
                if($v['tid']){
                    $banquet_type = db::name('banquet_types')->where('id',$v['tid'])->field('id,name')->find();
                    if($banquet_type){
                        $order[$k]['banquet_type'] = urldecode($banquet_type['name']);
                    }
                }
                $order[$k]['scheduled_time'] = date('Y-m-d H:i:s',$v['addtime']);

                if($v['begin_night'] == 1){
                    $night = '上午';
                }else{
                    $night = '晚上';
                }
                if($v['is_meeting'] == 1){
                    if($v['end_night'] == 1){
                        $nights = '上午';
                    }else{
                        $nights = '晚上';
                    }
                    $order[$k]['start_time'] = $v['begin_year'] .'-'. $v['begin_month'] .'-'. $v['begin_day'] . $night;
                    $order[$k]['end_time'] = $v['end_year'] .'-'. $v['end_month'] .'-'. $v['end_day'] . $nights;
                }else{
                    $order[$k]['start_time'] = $v['begin_year'] .'-'. $v['begin_month'] .'-'. $v['begin_day'] . $night;
                }
            }
        }
        $data['list'] = $order;
        $data['page'] = ceil($total/10);
        $data['total'] = $total;

        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 客户信息保存
     * @description 接口说明
     * @author 开发者
     * @url /api/Client/save
     * @method POST
     * @param name:id type:int require:1 default: other: desc:客户id
     * @param name:name type:string require:1 default: other: desc:名称
     * @param name:avatar type:int require:1 default: other: desc:头像（上传图片返回id）
     * @param name:unit type:string require:1 default: other: desc:单位
     * @param name:position type:string require:1 default: other: desc:职务
     * @param name:address type:string require:1 default: other: desc:住址
     * @param name:birthday type:string require:1 default: other: desc:生日
     * @param name:likes type:string require:1 default: other: desc:喜好
     * @param name:diet type:string require:1 default: other: desc:忌口
     * @param name:content type:string require:1 default: other: desc:备注
     * @param name:other type:int require:1 default: other: desc:其他（上传图片返回id）
     * @param name:sex type:int require:1 default: other: desc:性别1男2女
     */
    public function save()
    {
        $user = $this->user;
        if(!input('id') ){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');


        // if(!input('address') || !input('birthday') || !input('likes') || !input('diet') || !input('content') || !input('other')){
        //     return $this->ajaxReturn(1,'缺少参数');
        // }
        $id = input('id');
        $update['name'] = input('name');
        $update['avatar'] = input('avatar');
        $update['unit'] = input('unit');
        $update['position'] = input('position');
        $update['address'] = input('address');
        $update['birthday'] = input('birthday');
        $update['likes'] = input('likes');
        $update['diet'] = input('diet');
        $update['content'] = input('content');
        $update['other'] = input('other');
        $update['sex'] = input('sex');
        db::name('client')->where('id',$id)->update($update);
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 查看纪念日
     * @description 接口说明
     * @author 开发者
     * @url /api/Client/remember
     * @method POST
     * @param name:id type:int require:1 default: other: desc:客户id
     *
     * @return list:列表@
     * @list id:纪念日id cid:客户id relation:身份关系 remember:纪念类型
     */
    public function remember()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $page = input('page')??1;
        $where[] = ['cid','=',$id];
        // $list = db::name('remember')->where($where)->field('id,rid,cid,mid,day')->page("$page,10")->select();
        $list = db::name('remember')->where($where)->field('id,rid,cid,mid,day')->select();

        // $total = db::name('remember')->where($where)->count();
        if($list){
            foreach ($list as $k =>$v) {
                if($v['rid']){
                    $wheres[] = ['hid','=',$user['hid']];
                    $wheres[] = ['id','=',$v['rid']];
                    $wheres[] = ['type','=',1];
                    $types = db::name('remember_type')->where($wheres)->field('id,name')->find();
                    $list[$k]['relation'] = urldecode($types['name']);
                }
                if($v['mid']){
                    $wherea[] = ['hid','=',$user['hid']];
                    $wherea[] = ['type','=',2];
                    $wherea[] = ['id','=',$v['mid']];
                    $typea = db::name('remember_type')->where($wherea)->field('id,name')->find();
                    $list[$k]['remember'] = urldecode($typea['name']);
                }
            }
        }
        $data['list'] = $list;
        // $data['page'] = ceil($total/10);
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 删除纪念日
     * @description 接口说明
     * @author 开发者
     * @url /api/Client/del
     * @method POST
     * @param name:id type:int require:1 default: other: desc:纪念日id
     * 
     * 
     */
    public function del()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        db::name('remember')->where('id',$id)->delete();
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 新增纪念日
     * @description 接口说明
     * @author 开发者
     * @url /api/Client/increase
     * @method POST
     * @param name:id type:int require:1 default: other: desc:客户id
     * @param name:rid type:int require:1 default: other: desc:关系id
     * @param name:mid type:int require:1 default: other: desc:纪念类型id
     * @param name:day type:int require:1 default: other: desc:纪念日期     
     */
    public function increase()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        if(!input('rid')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        if(!input('mid')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        if(!input('day')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $data['cid'] = input('id');
        $data['rid'] = input('rid');
        $data['mid'] = input('mid');
        $data['day'] = input('day');
        db::name('remember')->insert($data);
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 获取身份关系纪念类型
     * @description 接口说明
     * @author 开发者
     * @url /api/Client/anniversary
     * @method POST
     * 
     * @return identity:列表@
     * @identity id:id name:名称
     *  @return type:纪念类型@
     * @type id:id name:名称
     */
    public function anniversary()
    {
        $user = $this->user;
        $where[] = ['is_hidden','=','1'];
        $where[] = ['hid','=',$user['hid']];
        $res = db::name('remember_type')->where($where)->where('type',1)->field('id,name')->select();
        $rea = db::name('remember_type')->where($where)->where('type',2)->field('id,name')->select();
        $list['identity'] = $res;
        $list['type'] = $rea;
        return $this->ajaxReturn(0,'成功',$list);
    }
}